1365C - Rotation Matching - CodeForces Solution


constructive algorithms data structures greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
 
 
  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
  int n;cin >> n;
   int a[n],b[n];
   map<int,int>indexa,indexb;
   for (int i = 0; i < n; i++)
   {
    cin >> a[i];
    indexa[a[i]]=i+1;
   }
   for (int i = 0; i < n; i++)
   {
    cin >> b[i];
    indexb[b[i]]=i+1;
   }
  int left=1,right=1;
  vector<int>diffleft,diffright;
  for (int i = 1; i <= n; i++)
  {
    int temp_sum=0;
    if(indexb[i]>indexa[i]){
       temp_sum+=(n-indexb[i]+indexa[i]);
       diffleft.push_back(temp_sum);
    }
    else
    {
      diffleft.push_back(indexa[i]-indexb[i]);
    } 
  }
  sort(diffleft.begin(),diffleft.end());
  int count=1;
  for (int i = 1; i < diffleft.size(); i++)
  {
    if (diffleft[i]!=diffleft[i-1])
    {
     left = max(left,count);
     count=1; 
    }
    else
    {
      count++;
    }
  }
  if(count!=1)left= max(left,count);
  for (int i = 1; i <= n; i++)
  {
    if (indexa[i]>indexb[i])
    {
      diffright.push_back(n-indexa[i]+indexb[i]);
    }
    else
    {
      diffright.push_back(indexb[i]-indexa[i]);
    }
  }
  sort(diffright.begin(),diffright.end());
  count=1;
  for (int i = 1; i < diffright.size(); i++)
  {
    if(diffright[i]!=diffright[i-1]){
     right= max(count,right);
     count=1;
    }
    else
    {
      count++;
    } 
  }
  if(count!=1)right = max(right,count);
  int ans = max(left,right);
  map<int,int>diffa,diffb;
  for (int i = 0; i < diffleft.size(); i++)
  {
    diffa[diffleft[i]]++;
  }
  for (int i = 0; i < diffright.size(); i++)
  {
    diffb[diffright[i]]++;
  }
  int  mini = n;
  for (int i = 0; i < diffleft.size(); i++)
  {
    if (diffa[diffleft[i]]==ans)
    {
      mini = min(mini,diffleft[i]);
      break;
    }
    
  }
  for (int i = 0; i < diffright.size(); i++)
  {
    if (diffb[diffright[i]]==ans)
    {
      mini = min(mini,diffright[i]);
      break;
    }
    
  }
 cout << ans << endl;

  
  return 0;  
}


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push